import {ref, computed, watch, reactive} from 'vue'
import { defineStore } from 'pinia'
import { SETTING_PREFIX } from '@/data/constants/Constants'
import { getTime } from '@/utils/timeUtils'
import { get, set } from '@/data/persistent/localStorage'
import {Settings, LogSetting, ExcelKeySetting, LogFormatSetting} from "@/data/entity/Settings";
import { Log } from "@/data/entity/Log";

export const useSettingsStore = defineStore('settings', () => {

    const settings = reactive(Settings.parse(get(SETTING_PREFIX + SETTING_NAME)))

    const defaultLog = computed(() => {
        settings.defaultLogSetting.date = getTime(settings.logSetting.date)
        return settings.defaultLogSetting
    })

    function resetLogSetting(){
        settings.defaultLogSetting = new Log()
        settings.logSetting = new LogSetting()
    }

    function resetExcelKeySetting(){
        settings.excelKeySetting = new ExcelKeySetting()
    }

    function resetLogFormatSetting(){
        settings.logFormatSetting = new LogFormatSetting()
    }

    watch(
        settings,
        async (settings)=>{
            set(SETTING_PREFIX + SETTING_NAME, settings)
        },
        {deep:true})

    return { settings, defaultLog, resetLogSetting, resetExcelKeySetting, resetLogFormatSetting }
})

const SETTING_NAME = 'settings'
